﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Threading;

namespace WebProxyApplication
{
    public static class MimeTypeHelper
    {
        private static readonly char[] sperators = new char[] { ',' };

        private volatile static Dictionary<string, string> mimeTypeDictionary = null;
        private volatile static object mimeTypeDictionaryLock = new object();

        private static Dictionary<string, string> GetMimeTypeDictionary()
        {
            string mimeType;
            string extention;
            string line;
            string[] splitLine;
            string mimeTypeFilePath;
            Dictionary<string, string> localMimeTypeDictionary;

            if (mimeTypeDictionary == null)
            {
                //lock (typeof(MimeTypeHelper))
                lock (mimeTypeDictionaryLock)
                {
                    if (mimeTypeDictionary == null)
                    {
                        localMimeTypeDictionary = new Dictionary<string, string>();

                        mimeTypeFilePath = HttpContext.Current.Server.MapPath("~/Data/MimeType.txt");
                        using (StreamReader sr = new StreamReader(mimeTypeFilePath))
                        {
                            while ((line = sr.ReadLine()) != null)
                            {
                                splitLine = line.Split(sperators, StringSplitOptions.RemoveEmptyEntries);
                                if (splitLine.Length == 2)
                                {
                                    extention = "." + splitLine[0];
                                    mimeType = splitLine[1];
                                    if (!localMimeTypeDictionary.Keys.Contains<string>(extention))
                                    {
                                        localMimeTypeDictionary.Add(extention, mimeType);
                                    }
                                    else
                                    {
                                        ProxyLogger.Log.WarnFormat("extension {0} has already been added", extention);
                                    }
                                }
                                else
                                {
                                    ProxyLogger.Log.WarnFormat("Could not split line contents '{0}' in file {1}", line, mimeTypeFilePath);
                                }
                            }
                        }
                        ProxyLogger.Log.InfoFormat("File {0} has been read and loaded into memory", mimeTypeFilePath);
                        mimeTypeDictionary = localMimeTypeDictionary;
                    }
                } // end locked section
            }
            return mimeTypeDictionary;
        }

        public static string DetermineMimeTypeFromFileExtension(Uri uri)
        {
            string mimeType = String.Empty;
            string extension;

            if (Path.HasExtension(uri.AbsolutePath))
            {
                extension = Path.GetExtension(uri.AbsolutePath);
                if (!GetMimeTypeDictionary().TryGetValue(extension, out mimeType))
                {
                    ProxyLogger.Log.WarnFormat("No mime type found for extention {0}", extension);
                }
            }
            else
            {
                ProxyLogger.Log.WarnFormat("No extension found for uri path {0}", uri.AbsolutePath);
            }

            return mimeType;
        }
    }
}